 /**
  ********************************************
  Linked List Class
  ********************************************
  
 * This program uses "LinkedList" in conjucation with "node" to complete Homework 3
 * 
 * This class comes with a "removeFront" method
 * 
 * @author (Greg Shevach & Chris Nerf) 
 * @version (v1.01)
 * @revision added error throwing for better error handling in .front() and .removeFront(), otherwise undefined behavior on empty list
 * @revision added fix with .removeFront() which caused nullpointerException error to be thrown
 * @date (9/27/11)
 */
         
public class LinkedList<T> {
            
    private Node<T> head;
    private Node<T> tail;
    private int size;

    public LinkedList() {
    head = null;
    tail = null;
    size = 0;
    }

    public void addRear( T element ) {
    Node<T> temp = new Node<T>( element );

    if( isEmpty() )
        head = temp;
    else
        tail.setNext( temp );

    tail = temp;
    ++size;
    }


    public T front() throws java.lang.RuntimeException //added error throwing, instead of just a string error message
    {
    T result = null;
    if( isEmpty() ) //@Greg the way it was would, printing a string if it is empty then continuing return result would cause undefined behavior.
        throw new java.lang.RuntimeException("The Linked List is empty");
    else
        result = head.getElement();
    return ( result );
    }

    public boolean isEmpty() {
    return ( size == 0 );
    }

    public int size() {
    return ( size );
    }

    public String toString() {
    String s = new String();
    Node<T> mover = head;
    while( mover != null ) {
        s += ( (mover.getElement()).toString() + " " );
        mover = mover.getNext();
    }
    return ( s );
    }
    
    public T removeFront() throws java.lang.RuntimeException //added error throwing, instead of just a string error message
    {  //Removes the first element of the linked list
    		if(isEmpty())//If list is empty, system message
    		      throw new java.lang.RuntimeException("The Linked List is empty"); //@Greg the way it was would, printing a string if it is empty then continuing return result would cause undefined behavior.
    		   
    Node<T> tempHead = this.head.getNext(); //get the location of the next element
   T result = this.front(); // this is the value of the first element
    this.head.remove(this.front());  //Removes the first element
    this.head= tempHead;                   //sets the head to the next element
   
    
   
    
    return ( result );      //Returns what was removed
    
  }
  
}